//
//  SMUserInfoModel.m
//  SaveMoney
//
//  Created by user on 2018/11/27.
//  Copyright © 2018年 Sunflower. All rights reserved.
//

#import "SMUserInfoModel.h"

#import "NSString+MD5.h"
#import "NSData+Encryption.h"

static NSString *const AESSecretKey = @"U4hdpztJJHPebYIYIcY8MgX8SHdTxVoO";
static NSString *const Md5Salt = @"ZsoJYFya";

@implementation SMUserInfoModel

+ (instancetype)sharedStorage {
    static SMUserInfoModel *storage;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        storage = [[self alloc] init];
    });
    return storage;
}

- (void)save {
    [[NSUserDefaults standardUserDefaults] synchronize];
}

- (void)clear {
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    
    [userDefaults removeObjectForKey:[self keyForProperty:@"userKey"]];
    [userDefaults removeObjectForKey:[self keyForProperty:@"addtime"]];
    [userDefaults removeObjectForKey:[self keyForProperty:@"avatar"]];
    [userDefaults removeObjectForKey:[self keyForProperty:@"email"]];
    [userDefaults removeObjectForKey:[self keyForProperty:@"nickName"]];
    [userDefaults removeObjectForKey:[self keyForProperty:@"phone"]];
    [userDefaults removeObjectForKey:[self keyForProperty:@"password"]]; 
    [userDefaults removeObjectForKey:[self keyForProperty:@"isLogin"]];
    
    [[NSUserDefaults standardUserDefaults] synchronize];
}

- (void)setUserKey:(NSString *)userKey {
    [self saveString:userKey forKey:@"userKey"];
}
- (NSString *)userKey {
    return [self stringForKey:@"userKey"];
}
- (void)setAddtime:(NSString *)addtime {
    [self saveString:addtime forKey:@"addtime"];
}
- (NSString *)addtime {
    return [self stringForKey:@"addtime"];
}
- (void)setAvatar:(NSString *)avatar {
    [self saveString:avatar forKey:@"avatar"];
}
- (NSString *)avatar {
    return [self stringForKey:@"avatar"];
}
- (void)setEmail:(NSString *)email {
    [self saveString:email forKey:@"email"];
}
- (NSString *)email {
    return [self stringForKey:@"email"];
}
- (void)setNickName:(NSString *)nickName {
    [self saveString:nickName forKey:@"nickName"];
}
- (NSString *)nickName {
    return [self stringForKey:@"nickName"];
}
- (void)setPhone:(NSString *)phone {
    [self saveString:phone forKey:@"phone"];
}
- (NSString *)phone {
    return [self stringForKey:@"phone"];
}
- (void)setPassword:(NSString *)password {
    [self saveString:password forKey:@"password"];
}
- (NSString *)password {
    return [self stringForKey:@"password"];
}
- (void)setIsLogin:(NSString *)isLogin {
    [self saveString:isLogin forKey:@"isLogin"];
}
- (NSString *)isLogin {
    return [self stringForKey:@"isLogin"];
}

- (void)setOldPhone:(NSString *)oldPhone {
    [self saveString:oldPhone forKey:@"oldPhone"];
}
- (NSString *)oldPhone {
    return [self stringForKey:@"oldPhone"];
}

- (void)setOldPassword:(NSString *)oldPassword {
    [self saveString:oldPassword forKey:@"oldPassword"];
}
- (NSString *)oldPassword {
    return [self stringForKey:@"oldPassword"];
}

- (void)setIsSuccess:(NSString *)isSuccess {
    [self saveString:isSuccess forKey:@"isSuccess"];
}
- (NSString *)isSuccess {
    return [self stringForKey:@"isSuccess"];
}
#pragma mark - Private

- (void)saveString:(NSString *)object forKey:(id)key {
    if (object == nil || key == nil) {
        return;
    }
    [[NSUserDefaults standardUserDefaults] setObject:[self dataEncryptedForValue:[NSString stringWithFormat:@"%@",object]] forKey:[self keyForProperty:key]];
}

- (NSString *)stringForKey:(NSString *)key {
    NSData *data = [[NSUserDefaults standardUserDefaults] objectForKey:[self keyForProperty:key]];
    return [self stringDecryptedForData:data];
}

#pragma mark - Encrypt & Decrypt

- (NSString *)keyForProperty:(NSString *)property {
    // 对键值进行md5加密，防止他人拿到后猜测
    return [NSString md5From:[property stringByAppendingString:Md5Salt] isLowerCase:YES];
}

- (NSData *)dataEncryptedForValue:(NSString *)value {
    // 对内容进行AES加密，防止他人拿到后破解
    return [[value dataUsingEncoding:NSUTF8StringEncoding] AES256EncryptWithKey:AESSecretKey];
}

- (NSString *)stringDecryptedForData:(NSData *)data {
    return [[NSString alloc] initWithData:[data AES256DecryptWithKey:AESSecretKey] encoding:NSUTF8StringEncoding];
}


@end
